import time
import requests
from Common.handle_redis import RedisHelper
from Common.connect_to_device import *
from Common.hande_device import HandleDevices
from influxdb_client import Point
from Common.handle_influxdb import InfluxDBHandler
from Common.handle_path import PID_LOGS_DIR

do_devices = HandleDevices(CPDemo_pkgName=MID_pkgName, Server_pkgName=Server_pkgName)
do_influxdb = InfluxDBHandler()

while True:
    try:
        apm_pid_cpdemo_main, apm_pid_server_main, pid_cpdemo_main, pid_server_main = do_devices.handle_main_device(
            deviceId=device_1)
        apm_pid_cpdemo_sec, apm_pid_server_sec, pid_cpdemo_sec, pid_server_sec = do_devices.handle_main_device(
            deviceId=device_2)
        print(f"当前主屏Demo PID为:{pid_cpdemo_main},Server PID为:{pid_server_main}")
        print(f"当前副屏Demo PID为:{pid_cpdemo_sec},Server PID为:{pid_server_sec}")

        # 获取CPU和内存数据
        device_001_cpdemo_cpu = do_devices.get_cpu(apm_pid_cpdemo_main)["appCpuRate"]
        device_001_cpdemo_mem = do_devices.get_mem(apm_pid_cpdemo_main)
        device_001_server_cpu = do_devices.get_cpu(apm_pid_server_main)["appCpuRate"]
        device_001_server_mem = do_devices.get_mem(apm_pid_server_main)
        device_002_cpdemo_cpu = do_devices.get_cpu(apm_pid_cpdemo_sec)["appCpuRate"]
        device_002_cpdemo_mem = do_devices.get_mem(apm_pid_cpdemo_sec)
        device_002_server_cpu = do_devices.get_cpu(apm_pid_server_sec)["appCpuRate"]
        device_002_server_mem = do_devices.get_mem(apm_pid_server_sec)

        # 准备数据点列表
        measurements = [
            Point("device_001").tag("pid", "demo_pid").field("cpu", device_001_cpdemo_cpu),
            Point("device_001").tag("pid", "demo_pid").field("mem", device_001_cpdemo_mem),
            Point("device_001").tag("pid", "server_pid").field("cpu", device_001_server_cpu),
            Point("device_001").tag("pid", "server_pid").field("mem", device_001_server_mem),

            # 分割
            Point("device_002").tag("pid", "demo_pid").field("cpu", device_002_cpdemo_cpu),
            Point("device_002").tag("pid", "demo_pid").field("mem", device_002_cpdemo_mem),
            Point("device_002").tag("pid", "server_pid").field("cpu", device_002_server_cpu),
            Point("device_002").tag("pid", "server_pid").field("mem", device_002_server_mem),
        ]

        # 写入数据库
        do_influxdb.write_mul_data(measurements=measurements)
    except Exception as e:
        pass

# http://10.10.118.92:3000/d/0WEnYgeSz/dpjing-xiang?orgId=1
# adb shell am start com.arcvideo.sdk.demo/com.arcvideo.ivi.sdk.demo2.activity.UnicastActivity
# adb shell am start --user 999 --display 2 com.arcvideo.sdk.demo/com.arcvideo.ivi.sdk.demo2.activity.UnicastActivity
# adb shell am force-stop com.arcvideo.sdk.demo
